﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Gebra
{
    public class Multiply : Operand
    {
        public readonly Operand _left;
        public readonly Operand _right;

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="a_left">Left operand.</param>
        /// <param name="a_right">Right operand.</param>
        /// <exception cref="ArgumentNullException">Thrown if "<paramref name="a_left"/>" is null.</exception>
        /// <exception cref="ArgumentNullException">Thrown if "<paramref name="a_right"/>" is null.</exception>
        public Multiply(Operand a_left, Operand a_right)
        {
            #region Argument Validation

            if (a_left == null)
                throw new ArgumentNullException("a_left");

            if (a_right == null)
                throw new ArgumentNullException("a_right");

            #endregion

            _left = a_left;
            _right = a_right;
        }

        /// <summary>
        /// Evaluate this operand.
        /// </summary>
        /// <returns>Result of evaluation.</returns>
        public override Operand Evaluate()
        {
            var leftResult = _left.Evaluate();
            var rightResult = _right.Evaluate();

            if (leftResult is Number && rightResult is Number)
                return new Number((leftResult as Number).Value * (rightResult as Number).Value);

            return new Add(leftResult, rightResult);
        }
    }
}
